package com.ql.controller.sbglController;

import com.ql.pojo.sheBeiGuanLi.WeiXiu;
import com.ql.service.doctor.DoctorService;
import com.ql.service.sbglService.wxService;
import com.ql.utils.Constrants;
import com.ql.utils.DateUtils;
import com.ql.utils.PageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.portlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Controller
@RequestMapping("/whgl/")
public class WXController {
    @Resource
    private wxService wxService;
    @Resource
    private DoctorService doctorService;
    //获取维修信息
    @RequestMapping("getAll")
    public String getAll(@RequestParam(value = "mohuName",required = false)String mohuName,
                         Model model, PageHelper ph){
        if (mohuName == null || mohuName.equals("")){
            mohuName = "";
        }
        //add:查询中带入分页条件:currentPageNo page-size
        List<WeiXiu> weiXiuList=wxService.getAllWx(ph,mohuName);
        model.addAttribute("weiXiuList",weiXiuList);
        model.addAttribute("currentPageNo",ph.getCurrentPageNo());//当前页
        model.addAttribute("totalCount",ph.getTotalCount());//总条数
        model.addAttribute("totalPageCount",ph.getTotalPageCount());//总页数
        model.addAttribute("mohuName",mohuName);//模糊查询
        return "whgl";
    }
    //单个删除
    @RequestMapping("del/{wId}")
    @ResponseBody
    public String del(@PathVariable("wId")int wId){
        if(wxService.removeWX(wId)){
            String msg = "{\"retMsg\":true}";
            return msg;
        }else{
            String msg = "{\"retMsg\":false}";
            return msg;
        }
    }
    //批量删除
    @RequestMapping("delBatch")//ajax的访问路径
    @ResponseBody//显示是以json格式返回，如果不加  返回一个视图
    public String delBatch(@RequestParam("delList")String[] delList){
        String regEx="[^0-9]";//java里的正则表达式，验证是否是数字
        List<Integer> list = new ArrayList<>();//要传到service层的list
        String result="{\"msg\":\"failed\"}";//返回的json  默认删除失败
        if(delList.length>0) {//如果前台传来的值为空，则不进行service层的调用.则list的长度为0
            for (int i = 0; i < delList.length; i++) {
                Pattern p = Pattern.compile(regEx);//构造java里的正则表达式类
                Matcher m = p.matcher(delList[i]);//进行匹配   m的值为每次循环的值（数字）
                list.add(Integer.valueOf(m.replaceAll("").trim()));//替换所有空格和换行
            }
        }
        if(list.size()>0){//传入service的list  如果list不为空，则传入service  否则不进行数据库操作
            if(wxService.deleteBatch(list)){//进行service层的方法。如果删除成功  进入if  否者进入else
                result="{\"msg\":\"success\"}";//删除成功，修改返回的json
            }else{
                result="{\"msg\":\"failed\"}";
            }
        }
        return result;//返回json
    }

    //获取id查询维修信息
    @RequestMapping("see_wx")
    public String getSeeWX(@RequestParam(value = "wId",required = false)String wId,Model model){
        WeiXiu weiXius=wxService.getWXById(wId);
        model.addAttribute("weiXius",weiXius);
        return "see_wx";
    }

    //拿到新增信息表
    @RequestMapping("goAdd")
    public String goAdd(Model model){
        return "wx_add";
    }
    //    新增库存
    @RequestMapping("add")
    public String addGM(@ModelAttribute("wx")WeiXiu weiXiu, String WsqTime, HttpSession session, Model model)throws Exception{
        if(null==session.getAttribute(Constrants.USER_SESSION)){
            //调用增加方法
            weiXiu.setSqTime(DateUtils.parse(WsqTime));
            if(wxService.addNewWX(weiXiu)){
                return "sbglSuccess";
            }else{
//                return "redirect:getAll";
                return "sbglErrer";
            }
        }else {
            return "sbglSuccess";
        }
    }
    //修改拿到信息表
    @RequestMapping("goUpdate")
    public String goUpdate(@RequestParam("wId")String wId, Model model){
        WeiXiu weiXiu=wxService.getWXById(wId);
        model.addAttribute("weiXiu",weiXiu);
        return "wx_edit";
    }
    //修改维修
    @RequestMapping("update")
    public String modifyBySBKC(@RequestParam("wId")String wId,@RequestParam("wCode")String wCode,@RequestParam("wTpye")String wTpye,@RequestParam("wNumber")String wNumber,
                               @RequestParam("sqPerson")String sqPerson,@RequestParam("jianding")String jianding,@RequestParam("sqTime")String sqTime,
                               HttpSession session,Model model){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date shijian = new Date();
        try {
            shijian = simpleDateFormat.parse(sqTime);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        WeiXiu weiXiu=new WeiXiu(Integer.parseInt(wId),wCode,wTpye,Integer.parseInt(wNumber),shijian,jianding,sqPerson);
        if(wxService.updateWX(weiXiu)){
            return "sbglSuccess";
        }else {
            return "sbglErrer";
        }
    }
}
